import json
from typing import Dict
# from flask import jsonify


class InputsInfo:
    
    def __init__(self, json_data: Dict[str, object]):
        self.status = json_data.get("status", "NotMath")
        self.latex = json_data.get("latex", "")

    def to_dict(self)-> Dict:
        """
        Converts the struct info object to a dictionary.
        
        Returns:
            Dict: A dictionary containing the struct info's status and latex fields.
        """
        return {
            "status": self.status,
            "latex": self.latex
        }
    
    def to_string(self)->str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class QuestionType:
    
    def __init__(self, json_data: Dict[str, object]):
        self.main_type = json_data.get("main_type", "")
        self.sub_type = json_data.get("sub_type", "")

    def to_dict(self)-> Dict:
        """
        Converts the object's main_type and sub_type attributes into a dictionary.
        
        Returns:
            Dict: A dictionary containing the object's main_type and sub_type.
        """
        return {
            "main_type": self.main_type,
            "sub_type": self.sub_type
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class DetailMathInfo:

    def __init__(self, json_data: Dict[str, object]):
        self.level = json_data.get("level", "")
        self.education_stage = json_data.get("education_stage", "")
        self.category = json_data.get("category", "")
        self.question_type = QuestionType(json_data.get("question_type", {}))

    def to_dict(self)-> Dict:
        return {
            "level": self.level,
            "education_stage": self.education_stage,
            "category": self.category,
            "question_type": self.question_type.to_dict()
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class SoveStep:
    
    def __init__(self, json_data: Dict[str, object]):
        self.description = json_data.get("description", "")
        self.explanation = json_data.get("explanation", "")
        self.formula = json_data.get("formula", "")
        self.key_points = [key_point for key_point in json_data.get("key_points", [])]
        self.education_notes = json_data.get("education_notes", "")

    def to_dict(self)-> Dict:
        return {
            "description": self.description,
            "explanation": self.explanation,
            "formula": self.formula,
            "key_points": [key_point for key_point in self.key_points],
            "education_notes": self.education_notes
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class FinalAnswer:

    def __init__(self, json_data: Dict[str, object]):
        self.text = json_data.get("text", "")
        self.latex = json_data.get("latex", "")
        self.confidence = json_data.get("confidence", 0.0)
        self.explanation = json_data.get("explanation", "")
        self.educational_value = {
            "key_learnings":[key_learning for key_learning in json_data.get("educational_value", {}).get("key_learnings", [])],
            "common_mistakes":[common_mistake for common_mistake in json_data.get("educational_value", {}).get("common_mistakes", [])],
            "practice_suggestions":[practice_suggestion for practice_suggestion in json_data.get("educational_value", {}).get("practice_suggestions", [])]
        }

    def to_dict(self)-> Dict:
        return {
            "text": self.text,
            "latex": self.latex,
            "confidence": self.confidence,
            "explanation": self.explanation,
            "educational_value": {
                "key_learnings":[key_learning for key_learning in self.educational_value["key_learnings"]],
                "common_mistakes":[common_mistake for common_mistake in self.educational_value["common_mistakes"]],
                "practice_suggestions":[practice_suggestion for practice_suggestion in self.educational_value["practice_suggestions"]]
            }      
        }

    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class Verification:

    def __init__(self, json_data: Dict[str, object]):
        self.method = json_data.get("method", "")
        self.result = json_data.get("result", "")
        self.reliability = json_data.get("reliability", 0.0)

    def to_dict(self)-> Dict:
        return {
            "method": self.method,
            "result": self.result,
            "reliability": self.reliability
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class MathSolution:

    def __init__(self, json_data: Dict[str, object]):
        self.steps = [SoveStep(step) for step in json_data.get("steps", [])]
        self.verification = Verification(json_data.get("verification", {}))
    
    def to_dict(self)-> Dict:
        return {
            "steps": [step.to_dict() for step in self.steps],
            "verification": self.verification.to_dict()
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class SolverResults:
    def __init__(self, json_data: Dict[str, object]):
        self.final_answer = FinalAnswer(json_data.get("final_answer", {}))
        self.solution = MathSolution(json_data.get("solution", {}))

    def to_dict(self)-> Dict:
        return {
            "final_answer": self.final_answer.to_dict(),
            "solution": self.solution.to_dict()
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)



class SolveInputs:
    
    def __init__(self, math_problem: str, detail_info: DetailMathInfo):
        self.problem = math_problem
        self.level = detail_info.level
        self.education_stage = detail_info.education_stage
        self.category = detail_info.category
        self.question_type = {
            "main_type": detail_info.question_type.main_type,
            "sub_type": detail_info.question_type.sub_type
        }
    
    def to_dict(self)-> Dict:
        return {
            "problem": self.problem,
            "level": self.level,
            "education_stage": self.education_stage,
            "category": self.category,
            "question_type": self.question_type
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class MathKnowledgeCoreConcepts:
    
    def __init__(self, json_data: Dict[str, object]):
        self.definition = json_data.get("definition", "")
        self.intuition = json_data.get("intuition", "")
        self.key_properties = json_data.get("key_properties", "")

    def to_dict(self)-> Dict:
        return {
            "definition": self.definition,
            "intuition": self.intuition,
            "key_properties": self.key_properties
        }

    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class MathKnowledgeExample:

    def __init__(self, json_data: Dict[str, object]):
        self.problem = json_data.get("problem", "")
        self.solution= json_data.get("solution", "")
        self.key_insights = json_data.get("key_insights", "")

    def to_dict(self)-> Dict:
        return {
            "problem": self.problem,
            "solution": self.solution,
            "key_insights": self.key_insights
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class MathKnowledgeResult:

    def __init__(self, json_data: Dict[str, object]):
        self.topic = json_data.get("topic", "")
        self.difficulty_level = json_data.get("difficulty_level", "")
        self.prerequisites = json_data.get("prerequisites", "")
        self.core_concepts = MathKnowledgeCoreConcepts(json_data.get("core_concepts", {}))
        self.detailed_explanation = json_data.get("detailed_explanation", "")
        self.examples = [MathKnowledgeExample(example) for example in json_data.get("examples", [])]
        self.common_misconceptions = json_data.get("common_misconceptions", "")
        self.applications = json_data.get("applications", "")
        self.further_reading = json_data.get("further_reading", "")

    def to_dict(self)-> Dict:
        return {
            "topic": self.topic,
            "difficulty_level": self.difficulty_level,
            "prerequisites": self.prerequisites,
            "core_concepts": self.core_concepts.to_dict(),
            "detailed_explanation": self.detailed_explanation,
            "examples": [example.to_dict() for example in self.examples],
            "common_misconceptions": self.common_misconceptions,
            "applications": self.applications,
            "further_reading": self.further_reading
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


class CompleteInfo:
    
    def __init__(self, complete_reason: str, data_info: Dict[str, object] = None)->None:
        self.reason = complete_reason
        self.data_info = data_info

    def to_dict(self)-> Dict:
        return {
            "status": "COMPLETE",
            "reason": self.reason,
            "data_info": self.data_info if self.data_info else ""
        }
    
    def to_string(self)-> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)


